﻿using BrnMall.Manager.Domain.AddressBook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.AddressBook
{
    public class StaffDepartmentManager
    {
        static string TableName = "crm_StaffDepartment";

       
        public static bool Insert(StaffDepartment model)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(model);
            }
            return model.StaffId > 0;
        }

        public static List<StaffDepartment> GetMyDeportment(int staffId, int pageSize = 50)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<StaffDepartment>(string.Format("select top {0} * from {1} where staffId={2}", pageSize, TableName, staffId));
            }
        }

        #region AI统计,boss统计

        /// <summary>
        /// 获取我所在的部门员工
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<StaffDepartment> GetChildList(int staffId, int departmentId)
        {
            using (var db = new QDMallDatabase())
            {
                if (departmentId > 0)
                {
                    //按部门ID
                    return db.Fetch<StaffDepartment>($"select * from crm_staffDepartment where DepartmentId={departmentId}");
                }
                else
                {
                    //我的部门ID
                    return db.Fetch<StaffDepartment>($"select * from crm_staffDepartment where DepartmentId in (select DepartmentId from crm_staffDepartment t2 where t2.StaffId={staffId})");
                }
            }
        }
        #endregion

        /// <summary>
        /// 获取所有部门关系
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public static List<StaffDepartment> GetListByStoreId(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<StaffDepartment>(string.Format("select * from {0} where storeId={1}", TableName, storeId));
            }
        }


        public static List<StaffDepartment> GetListByAmount(int staffId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<StaffDepartment>(string.Format("select * from {0} where staffId={1}", TableName, staffId));
            }
        }


        public static int Add(StaffDepartment model)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute(string.Format("insert into {0}(storeid,staffid,departmentid) values({1},{2},{3})"
                    , TableName, model.StoreId, model.StaffId, model.DepartmentId));
            }
        }


        public static int Delete(int Id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute(string.Format("delete from {0} where Id={1}", TableName, Id));
            }
        }

        public static int DeleteByStaffId(int staffid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute(string.Format("delete from {0} where staffid={1}", TableName, staffid));
            }
        }


        public static List<StaffDepartment> GetStaffDepList(int page, int pageSize, int DepartmentId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<StaffDepartment>(page, pageSize, string.Format("select * from {0} where DepartmentId={1}", TableName, DepartmentId));
            }
        }
    }
}